package com.kjlink.internal.report4vue.jira.controller;

import cn.xuqiudong.common.base.model.BaseResponse;
import cn.xuqiudong.common.base.model.PageInfo;
import com.kjlink.internal.report4vue.base.controller.BaseApiController;
import com.kjlink.internal.report4vue.jira.lookup.ProjectJiraLookup;
import com.kjlink.internal.report4vue.jira.request.JiraImportVo;
import com.kjlink.internal.workreport.base.enums.ProjectType;
import com.kjlink.internal.workreport.helper.SessionHelper;
import com.kjlink.internal.workreport.jira.model.Jira;
import com.kjlink.internal.workreport.jira.service.JiraService;
import com.kjlink.internal.workreport.jira.vo.JiraReportDetail;
import com.kjlink.internal.workreport.project.model.Project;
import com.kjlink.internal.workreport.project.service.ProjectService;
import com.kjlink.internal.workreport.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述:
 *
 * @author Vic.xu
 * @see com.kjlink.internal.workreport.jira.controller.JiraController
 * @since 2024-12-14 20:44
 */
@RestController
public class ApiJiraController extends BaseApiController {

    @Resource
    private JiraService service;

    @Resource
    private ProjectService projectService;
    @Autowired
    private JiraService jiraService;


    /**
     * 列表页
     */
    @RequestMapping(value = "/jira/list", method = {RequestMethod.GET, RequestMethod.POST})
    public BaseResponse<PageInfo<Jira>> list(@RequestBody Jira lookup) {
        PageInfo<Jira> pager = service.page(lookup);
        return BaseResponse.success(pager);
    }


    /**
     * jira 号是否在本系统存在
     */
    @RequestMapping("/jira/checkCode/{id}/{code}")
    public BaseResponse<Boolean> checkCode(@PathVariable Integer id, @PathVariable String code) {
        if (id <= 0) {
            id = null;
        }
        boolean success = service.checkNotRepeat(id, code, "code");
        return BaseResponse.success(success);
    }

    /**
     * 检测jira号是否存在，先在本系统查询，后去jira系统查询
     */
    @RequestMapping("/jira/exist/{code}")
    public BaseResponse<Boolean> exist(@PathVariable String code) {
        boolean exist = service.checkJira(code);
        return BaseResponse.success(exist);
    }

    /**
     * 保存jira
     */
    @PostMapping("/jira/save")
    public BaseResponse<Integer> save(@RequestBody Jira entity) {
        int num = service.save(entity);
        return BaseResponse.success(num);
    }


    /**
     * 根据jira号导入jira
     */
    @PostMapping("/jira/import/{projectId}/{code}")
    public BaseResponse<Jira> importJira(@PathVariable Integer projectId, @PathVariable String code) {
        return service.createByCode(code, projectId, false);
    }

    /**
     * 一次导入多个jira号
     */
    @PostMapping("/jira/importJiras")
    public BaseResponse<String> importJiras(@RequestBody JiraImportVo vo) {
        if (vo.getProjectId() == null) {
            return BaseResponse.error("项目不能为空");
        }
        if (vo.getCodes() == null || vo.getCodes().size() == 0) {
            return BaseResponse.error("jira号不能为空");
        }
        List<String> failMsg = new ArrayList<>();
        Integer projectId = vo.getProjectId();
        for (String code : vo.getCodes()) {
            BaseResponse<Jira> response = service.createByCode(code, projectId, false);
            if (!response.isSuccess()) {
                failMsg.add(response.getMsg());
            }
        }
        String data = String.join("<br/>", failMsg);
        return BaseResponse.success(data);

    }

    /**
     * 批量更新jira项目id
     */
    @PostMapping("/jira/batchUpdateProjectId")
    public BaseResponse<Integer> batchUpdateProjectId(@RequestBody JiraImportVo vo) {
        int num = service.batchUpdateProjectId(vo);
        return BaseResponse.success(num);
    }


    @PostMapping("/jira/delete/{id}")
    public BaseResponse<Integer> deleteProjectUser(@PathVariable Integer id) {
        int delete = service.delete(id);
        return BaseResponse.success(delete);
    }

    /**
     * 根据jira号统计工时信息
     *
     * @param code jira号
     */
    @GetMapping("/jira/summary/{code}")
    public BaseResponse<Void> summary(@PathVariable String code) {
        service.summaryUsedTime(code);
        return BaseResponse.success();
    }

    /**
     * jira的工时详情 并进行统计一次工时
     */
    @GetMapping("/jira/reportDetail/{code}")
    public BaseResponse<JiraReportDetail> reportDetail(@PathVariable String code, String back, RedirectAttributes attr) {
        JiraReportDetail detail = service.summaryAndGetReportDetail(code);
        if (detail == null) {
            return BaseResponse.error("不存在的jira");
        }
        return BaseResponse.success(detail);
    }


    /**
     * 根据项目获取jira下拉列表
     * 新需求类型项目选项为：JIRA编号包含项目的JIRA前缀，JIRA经办人为当前用户，   JIRA类型为非BUG的所有JIRA。
     * 生产运维类型项目选项为：JIRA编号包含项目的JIRA前缀，JIRA经办人为当前用户，  JIRA类型为NOT in(New Feature、Improvement)
     */
    @GetMapping("/jira/selectByProjectId")
    public BaseResponse<List<Jira>> getJiraList(@RequestParam Integer projectId,
                                                @RequestParam(defaultValue = "false") boolean isSub) {
        Project project = projectService.findById(projectId);
        if (project == null) {
            return BaseResponse.success(new ArrayList<>());
        }
        ProjectType type = project.getType();
        //jira 前缀
        String prefix = project.getJiraPrefix();
        String assignee = null;
        User user = SessionHelper.currentUser();
        if (user != null) {
            assignee = user.getAssignee();
        }
        ProjectJiraLookup lookup = new ProjectJiraLookup(projectId, type, prefix, assignee, isSub);
        List<Jira> list = jiraService.getJiraList(lookup);
        return BaseResponse.success(list);
    }

    /**
     * 统计全部工时，慎用
     */
    @GetMapping("/jira/summaryAll")
    public BaseResponse<Integer> summaryAll() {
        int num = jiraService.summaryAll();
        return BaseResponse.success(num);

    }

    /**
     * 清空计划时间
     */
    @PostMapping("/jira/clearPlanDate/{id}")
    public BaseResponse<Integer> clearPlanDate(@PathVariable Integer id) {
        int num = jiraService.clearPlanDate(id);
        return BaseResponse.success(num);
    }

}
